﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.HU;
using HiStaff.Util;
using HiStaff.Dal;
using System.Windows.Forms;
using Janus.Windows.GridEX;

namespace HiStaff.iProfile
{
    public partial class Salary : FormBase
    {
        public Salary()
        {
            InitializeComponent();
        }

        protected override bool FormLoad()
        {
            try
            {
                AddBasicCommandBar();
                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
                BaseStatus = Util.Enum.FormStatus.View;
                SetStatusInputControl(this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        private void BindDataByEmp()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_HU_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = hiCheckBox1.Value });
                grdEmpList.DataSource = lstEmployee;
                grdEmpList.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_SALARY>("APRS_HU_SALARY", new EMPLOYEE_SEARCH { EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID")) });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void BindCostCenter()
        {
            try
            {
                var obj = new HU_COSTCENTER();
                obj.SALARYID = ((HU_SALARY)(grdMain.SelectedItems[0].GetRow().DataRow)).ID;
                obj.EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID"));
                var lstCostCenter = OracleHelper.ExcuteSelectMultiObject<HU_COSTCENTER>("PRS_HU_COSTCENTER", obj);
                grdCostcenter.DataSource = lstCostCenter;
                grdCostcenter.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiTreeOrganization1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                BindDataByEmp();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdEmpList_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (grdEmpList.GetValue("ID") == null)
                {
                    grdMain.DataSource = null;
                    grdMain.Refresh();
                    ResetInputControl(this);
                }
                else
                {
                    BindData();
                    BindCostCenter();
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                BindDataFromGridToForm(grdMain, this);
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                BindDataByEmp();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        protected bool CheckData()
        {
            try
            {
                if (!Common.CheckEffectDateAndExpireDate(txtSEffectDate, txtSExpireDate))
                {
                    MessageBox.Show(GetMessageById("MSG_INVALID_DATE"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool SaveButtonClick()
        {
            try
            {
                if (!CheckData())
                {
                    return false;
                }
                else
                {
                    var objSave = new HU_SALARY();
                    ValueControlsToObject(objSave, this);
                    objSave.EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID"));
                    if (BaseStatus == Util.Enum.FormStatus.Add)
                    {
                        objSave.ID = 0;
                    }
                    OracleHelper.ExcuteNonQuery("APRU_HU_SALARY", objSave);

                    //COST CENTER
                    var objCostCenter = new HU_COSTCENTER();
                    objCostCenter.SALARYID = objSave.ID;
                    OracleHelper.ExcuteNonQuery("APRD_HU_COSTCENTER", objCostCenter);
                    foreach (GridEXRow emp in grdCostcenter.GetDataRows())
                    {
                        if (emp.RowType == RowType.Record && Convert.ToDecimal(emp.Cells["PERCENT"].Value) > 0)
                        {
                            objCostCenter.COSTCENTERID = Convert.ToDecimal(emp.Cells["ID"].Value);
                            objCostCenter.PERCENT = Convert.ToDecimal(emp.Cells["PERCENT"].Value);

                            OracleHelper.ExcuteNonQuery("APRU_HU_COSTCENTER", objCostCenter);
                        }
                    }

                    BindData();
                    MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool CancelButtonClick()
        {
            try
            {
                BindDataFromGridToForm(grdMain, this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool DeleteButtonClick()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (OracleHelper.ExcuteNonQuery("APRD_HU_SALARY", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        return true;
                    }
                }
            }
            return false;
        }

        private void cboPC_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cboPC.SelectedValue == null)
                {
                    txtMin.Value = null;
                    txtMax.Value = null;
                }
                else
                {
                    txtMin.Value = ConvertHelper.ToDecimal(((System.Data.DataRowView)(cboPC.SelectedItem.DataRow)).Row.ItemArray[3]);
                    txtMax.Value = ConvertHelper.ToDecimal(((System.Data.DataRowView)(cboPC.SelectedItem.DataRow)).Row.ItemArray[4]);
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void TotalCost()
        {
            try
            {
                txtTotalSal.Value = txtSalary.Value * txtPercentSal.Value / 100;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void txtSalary_Leave(object sender, EventArgs e)
        {
            try
            {
                if (txtSalary.Value > 0 && (txtMin.Value > txtSalary.Value || txtSalary.Value > txtMax.Value))
                {
                    if (MessageBox.Show("Mức lương không nằm trong khoảng Min-Max. Tiếp tục?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.Cancel)
                    {
                        txtSalary.Value = 0;
                    }
                    else
                    {
                        TotalCost();
                    }
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void txtSalary_ValueChanged(object sender, EventArgs e)
        {
            TotalCost();
        }

        private void txtPercentSal_ValueChanged(object sender, EventArgs e)
        {
            TotalCost();
        }
    }
}
